package com.sddongyun.proxy;

import com.sddongyun.common.RequestWrapper;
import com.sddongyun.common.ResponseWrapper;
import com.sddongyun.common.utils.HttpClientUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.message.BasicHttpRequest;
import org.apache.http.util.EntityUtils;
import org.mitre.dsmiley.httpproxy.ProxyServlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 重写 医保 代理
 * @author Lenovo
 * @date 2021-04-1815:20
 */
@Slf4j
public class YbHttpProxyServlet extends ProxyServlet {

    @Override
    protected void service(HttpServletRequest servletRequest, HttpServletResponse servletResponse) throws ServletException, IOException {
//        service((RequestWrapper)servletRequest, (ResponseWrapper)servletResponse);

        RequestWrapper requestWrapper = (RequestWrapper)servletRequest ;

        if (servletRequest.getAttribute(ATTR_TARGET_URI) == null) {
            servletRequest.setAttribute(ATTR_TARGET_URI, this.targetUri);
        }
//        String url = "http://192.168.95.122:8080/yb";
//        String url = "http://10.34.17.208:8090/services/YtjServer";
//        String url = "http://192.168.95.122:8082/yb";
//        String url = "http://localhost:8083/yb";
        String url = this.targetUri ;

        Map<String, String> headerMap = new HashMap<>();
        headerMap.put("content-type","text/html;charset=utf-8");
//        String jsonParam = "<soapenv:Envelope xmlns:soapenv =\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsd =\"http://www.ylzinfo.com/xsd\"><soapenv:Header/><soapenv:Body><xsd:ydjy_common_register><xsd:username>CDYTJ02</xsd:username><xsd:password >7f6d6b863be541677e464308fd7440dc</xsd:password ><xsd:machineid >zzj003</xsd:machineid ><xsd:xmlIn ><![CDATA[<soap:envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" soap:encodingstyle =\"http://schemas.xmlsoap.org/soap/encoding/\"><soap:header><in:system xmlns:in=\"http://www.ylzinfo.com/\"><para usr =\"CDYTJ02\"/><para pwd =\"7f6d6b863be541677e464308fd7440dc\"/><para machineid =\"zzj003\"/><para sid =\"ydjy_common_register\"/></in:system></soap:header><soap:body><in:business xmlns:in=\"http://www.ylzinfo.com/\"><para akb020=\"02\"/><para aac002=\"130802196909230426\"/><para aac003=\"牛伯华\"/><para ckc110=\"144\"/><para akc193=\"\"/><para aae005=\"\"/><para aae006=\"\"/><para akc194=\"\"/><para aae011=\"ytjzz\"/><para xae011=\"一体机自助\"/><para skc098=\"000\"/><paralist name=\"list01\"><row akc227=\"15\" ckc169=\"呼吸内科门诊\" aae011=\"一体机自助\" cke086=\"\" cke089=\"1\" ska003=\"001\" ckc171=\"呼吸内科门诊\" ckc170=\"普通号\" cke081=\"P\" aac001=\"130802196909230426\" akc220=\"2001||7513\" ckc158=\"1\" akc222=\"LS15011409\" akc223=\"普通号\" ckc197=\"15\" ckc198=\"1\" cke090=\"000\" ckc159='/' ckc160='/' ckc161=\"001102000010000-11020000100\" cke085=\"\" aka063=\"P\"></row></paralist></in:business></soap:body></soap:envelope>]]></xsd:xmlIn ></xsd:ydjy_common_register></soapenv:Body></soapenv:Envelope>";
        String jsonParam = requestWrapper.getBodyString();
        String rtn = "";
        try {
            rtn = HttpClientUtil.doPost(url,jsonParam,headerMap);

        } catch (Exception e) {
            e.printStackTrace();
            rtn =
                    "<resultset name=\"structs\">0</resultset>" +
                            "<resultset name=\"retrieve\" information=\"ORA-错误:"+e.getMessage()+"\"></resultset>";
        }
        servletResponse.getOutputStream().write(rtn.getBytes("utf-8"));

    }

}
